package com.example.evanproject.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.evanproject.config.UpdateImg;
import com.example.evanproject.domain.Course;
import com.example.evanproject.domain.Register;
import com.example.evanproject.domain.Studentcourses;
import com.example.evanproject.mapper.ExcellentMapper;
import com.example.evanproject.mapper.RegisterMapper;
import com.example.evanproject.mapper.StudentcoursesMapper;
import com.example.evanproject.service.CourseService;
import com.example.evanproject.mapper.CourseMapper;
import com.example.evanproject.util.EntityMapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
* @author hanxiaoqi
* @description 针对表【course】的数据库操作Service实现
* @createDate 2024-03-09 21:52:12
*/
@Service
public class CourseServiceImpl extends ServiceImpl<CourseMapper, Course>
    implements CourseService{
    @Autowired
    CourseMapper courseMapper;
    public List<Course> selecttest(){
        return courseMapper.selecttest();
    }
    public Course selectListById(Integer cId) {

        return courseMapper.selectListById(cId);
    }

    @Autowired
    StudentcoursesMapper studentCoursesMapper;
    @Autowired
    RegisterMapper registerMapper;

    @Autowired
    ExcellentMapper excellentMapper;

    //获取个人主页的课程
    public List<Course> selectcoursebyuserid(Integer userid){
        List<Studentcourses> list=studentCoursesMapper.selectbuUserid(userid);
        List<String> userids=new ArrayList<>();
        //如果查询出来没有数据要直接返回一个空数组，后面是in查询 不能是空
        for (int i=0;i<list.size();i++){
            userids.add(String.valueOf(list.get(i).getCourseId()));
        }
        if(userids.size()<=0){
            Course course=new Course();
            List<Course> list1=new ArrayList<>();
            return list1;
        }
        String[] ids=userids.toArray(new String[userids.size()]);
        List<Course> couserlist=courseMapper.selectbyids(ids);
        System.out.println(couserlist);
        return couserlist;
    }


    //    //    获取老师的全部课程（以及携带参加的学生的人数）
    public List<Map> allcoursebyteacherid(Integer tid) {
        //首先获取老师的课程 再次循环将人数填进去

        List<Course> courseList=courseMapper.selectByTid(tid);
        List<Map> list=new ArrayList<>();
        for(int i=0;i<courseList.size();i++){
            Integer courseid=courseList.get(i).getCourseId();
//           这个集合长度就是参与的学生的人数
            List<Studentcourses> studentCourses= studentCoursesMapper.selectbycourseidstudent(courseid);
//            System.out.println(studentCourses);
            Map<String,Object> map = EntityMapUtils.entityToMap(courseList.get(i));
            map.put("addnum",studentCourses.size());
            list.add(map);


        }

        System.out.println(courseList);

        return list;
    }
    //  查询单个课程信息以及带有参加的学生的信息
    public Map coursebyteacherid(Integer courseid) {
        Course course=courseMapper.selectListById(courseid);
        List<Studentcourses> studentCourses= studentCoursesMapper.selectbycourseidstudent(course.getCourseId());
//            将课程里面对应的学生id提取为一个数组(不用循环)
        Integer[] sId = studentCourses.stream().map(Studentcourses::getSId).toArray(Integer[]::new);
//            System.out.println(ids);
        //如果有参加的学生人数 才进行查询(返回具体的信息)
        List<Register> registerslist=new ArrayList<>();
        if(sId.length>0){
            registerslist =registerMapper.morepople(sId);
        }
        Map<String,Object> map = EntityMapUtils.entityToMap(course);
        map.put("studentlist",registerslist);
        return map;
    }

    public Integer addcourselist(String courseName, Integer tId, String courseNote, String courseStarttime, String courseEndtime, Integer courseTyid, Integer courseMoney, MultipartFile file) {

        String courseImg= UpdateImg.updateimg(file);
        return  courseMapper.addcourselist(courseName,tId,courseNote,courseStarttime,courseEndtime,courseTyid,courseMoney,courseImg);
    }

    public Integer delectcoursebyid(Integer courseId) {
        return  courseMapper.delectcoursebyid(courseId);
    }

    public List<Course> selectrotation(){
        String str=excellentMapper.selectgoodall(1);
        String[] arr={"0"};
        if(str!=null&&str!=""){
            arr= str.split(",");

        }
        return  courseMapper.selectbyids(arr);
    }

    @Override
    public List<Course> couserBarnk() {
        return courseMapper.couserBarnk();
    }

    @Override
    public R<IPage<Course>> selectall2(Integer pageNum, Integer pageSize, String courseName) {
        QueryWrapper<Course> wrapper=new QueryWrapper<>();
        wrapper.lambda().like(courseName!=null&&!courseName.equals(""),Course::getCourseName,courseName);
        IPage<Course> page=new Page<>();
        page.setCurrent(Long.valueOf(pageNum));
        page.setSize(Long.valueOf((pageSize)));
        IPage<Course> pages = courseMapper.selectPage(page, wrapper);
        return R.ok(pages);
    }
}




